package com.winspread.ui.squareview.base

import android.content.Context
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import androidx.annotation.Nullable
import com.winspread.ui.R

/**
 *Author: Amin
 *Data: 2022/4/11 10:24
 *
 *Description: 副布局样式
 *
 * 如 按下状态，点击状态时候的 弧度，背景
 *
 */
class SquareAssistantView(
    @Nullable context: Context?,
    @Nullable attrs: AttributeSet?,
    var squareNormalView: SquareNormalView,
) {


    private var corner: Float = 0F

    var cornerLt: Float
    var cornerLb: Float
    var cornerRt: Float
    var cornerRb: Float

    var dashWidth: Float = 0F
    var dashGap: Float = 0F
    var lineColor: Int = 0
    var fillColor: Int = 0
    var lineWidth: Float = 0F

    var textColor: Int? = null

    // 图片
    var topDraw: Drawable? = null
    var leftDraw: Drawable? = null
    var bottomDraw: Drawable? = null
    var rightDraw: Drawable? = null


    fun setCorner(value: Float) {
        corner = value
        cornerLt = 0F
        cornerLb = 0F
        cornerRt = 0F
        cornerRb = 0F
    }


    init {

        val obtainStyledAttributes =
            context!!.obtainStyledAttributes(attrs, R.styleable.View)
        //圆角
        corner =
            obtainStyledAttributes.getDimension(R.styleable.View_focused_corner, squareNormalView.getCorner())
        cornerLt =
            obtainStyledAttributes.getDimension(R.styleable.View_focused_corner_left_top, squareNormalView.cornerLt)
        cornerLb = obtainStyledAttributes.getDimension(
            R.styleable.View_focused_corner_left_bottom,
            squareNormalView.cornerLb
        )
        cornerRt = obtainStyledAttributes.getDimension(
            R.styleable.View_focused_corner_right_top,
            squareNormalView.cornerRt
        )
        cornerRb = obtainStyledAttributes.getDimension(
            R.styleable.View_focused_corner_right_bottom,
            squareNormalView.cornerRb
        )
        //线宽
        lineWidth =
            obtainStyledAttributes.getDimension(R.styleable.View_focused_line_width, squareNormalView.lineWidth)
        //虚线宽度
        dashWidth =
            obtainStyledAttributes.getDimension(R.styleable.View_focused_dash_width, squareNormalView.dashWidth)
        // 虚线间距
        dashGap = obtainStyledAttributes.getDimension(R.styleable.View_focused_dash_gap, squareNormalView.dashGap)
        // 线颜色
        lineColor = obtainStyledAttributes.getColor(
            R.styleable.View_focused_line_color,
            squareNormalView.lineColor
        )
        // 填充颜色
        fillColor = obtainStyledAttributes.getColor(
            R.styleable.View_focused_background_color,
            squareNormalView.fillColor
        )
        textColor = obtainStyledAttributes.getColor(
            R.styleable.View_focused_text_color,
            squareNormalView.textColor
        )
        // draw 图片
        topDraw = obtainStyledAttributes.getDrawable(
            R.styleable.View_focused_drawable_top)
        topDraw ?: let {
            topDraw = squareNormalView.topDraw
        }
        leftDraw = obtainStyledAttributes.getDrawable(
            R.styleable.View_focused_drawable_left)
        leftDraw ?: let {
            leftDraw = squareNormalView.leftDraw
        }
        rightDraw = obtainStyledAttributes.getDrawable(
            R.styleable.View_focused_drawable_right)
        rightDraw ?: let {
            rightDraw = squareNormalView.rightDraw
        }
        bottomDraw = obtainStyledAttributes.getDrawable(
            R.styleable.View_focused_drawable_bottom)
        bottomDraw ?: let {
            bottomDraw = squareNormalView.bottomDraw
        }
    }


    fun getBackGround(): Drawable {
        var drawableBuilder = DrawableBuilder().corner()
        if (squareNormalView.defValue != corner) {
            drawableBuilder = drawableBuilder.corner(
                floatArrayOf(
                    corner, corner, corner, corner,
                    corner, corner, corner, corner
                )
            )
        }

        if (squareNormalView.defValue != cornerLt || squareNormalView.defValue != cornerLb
            || squareNormalView.defValue != cornerRt || squareNormalView.defValue != cornerRb
        ) {
            drawableBuilder =
                drawableBuilder.corner(
                    floatArrayOf(
                        cornerLt, cornerLt, cornerRt, cornerRt,
                        cornerRb, cornerRb, cornerLb, cornerLb
                    )
                )
        }

        if (squareNormalView.defValue != lineWidth) {
            drawableBuilder = drawableBuilder.line(lineWidth, lineColor)
        }

        if (squareNormalView.defValue != dashWidth) {
            drawableBuilder = drawableBuilder.dash(dashWidth, dashGap)
        }

        if (fillColor != 0) {
            drawableBuilder = drawableBuilder.fill(fillColor)
        }
        return drawableBuilder.build()
    }
}